package com.probiz.estore.sales.service.impl;

import java.util.List;

import com.probiz.estore.common.model.sales.PromoRuleElement;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.sales.service.PromoRuleElementManager;
import com.probiz.estore.sales.dao.PromoRuleElementDao;


/**
 * Manager implementation for PromoRuleElement, responsible for business processing, and communicate between web and persistence layer.
 */
public class PromoRuleElementManagerImpl extends GenericManagerImpl<PromoRuleElement> implements PromoRuleElementManager {

	private PromoRuleElementDao promoRuleElementDao = null;

	/**
	 * @param promoRuleElementDao
	 *            the promoRuleElementDao to set
	 */
	public void setPromoRuleElementDao(PromoRuleElementDao promoRuleElementDao) {
		this.promoRuleElementDao = promoRuleElementDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = promoRuleElementDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(PromoRuleElement entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(PromoRuleElement entity) {

	}
	public void deleteAllByRuleId(Integer _ruleId){
		promoRuleElementDao.deleteAllByRuleId(_ruleId);
	}

	public List<PromoRuleElement> findAllActionByRuleId(Integer id) {
		String hql="from PromoRuleElement element where element.kind='action' and element.promoRule.promoRuleId = ?";
		List<PromoRuleElement> elementList = (List<PromoRuleElement>)promoRuleElementDao.findByHql(hql, id);
		if (elementList.size() > 0) {
			return elementList;
		} else {
			return null;
		}
	}
	
	public List<PromoRuleElement> findAllByRuleId(Integer _ruleId){
		String hql="from PromoRuleElement element where element.kind='condition' and element.promoRule.promoRuleId = ?";
		List<PromoRuleElement> elementList = (List<PromoRuleElement>)promoRuleElementDao.findByHql(hql, _ruleId);
		if (elementList.size() > 0) {
			return elementList;
		} else {
			return null;
		}
		
	}
}
